package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.TheSetmealHasNoStatusDish;
import com.sky.exception.TheSetmealIsBuying;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealServise;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiseImpl implements SetmealServise {
    @Autowired
    private SetmealMapper setmealMapper;

    //修改套餐
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);
        setmealMapper.deleteSetmealDish(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
            setmealMapper.addSetmealDish(setmealDish);
        }


    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPage) {
        PageHelper.startPage(setmealPage.getPage(),setmealPage.getPageSize());
        List<SetmealVO> setmeals = setmealMapper.selectByPage(setmealPage);
        Page<SetmealVO> p = (Page<SetmealVO>) setmeals;
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public void setmealStartOrStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        //判断套餐中是否包含未起售商品 包含未起售商品不能起售
        if(status == 1){
            //通过套餐id查询套餐下菜品id集合
            List<Integer> integers = setmealMapper.selectDishIDById(id);

            //通过菜品id查询菜品是否起售
            for (Integer integer : integers) {
                Integer status1 = setmealMapper.selectDishStatus(integer);
                if (status1 == 0){
                    throw new TheSetmealHasNoStatusDish();
                }
            }

        }
        setmealMapper.setmealStartOrStop(setmeal);
    }

    @Override
    public void deleteSetmeal(List<Long> ids) {
        for (Long id : ids) {
            //查看套餐是否起售 起售的套餐不能删除
            Integer status = setmealMapper.selectStatus(id);
            if (status == 1){
                throw new TheSetmealIsBuying("起售中的套餐不能删除");
            }
            setmealMapper.deleteSetmeal(id);
            setmealMapper.deleteSetmealDish(id);
        }
    }

    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        //添加套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addSetmeal(setmeal);
        setmealDTO.getSetmealDishes().get(0).setSetmealId(setmeal.getId()); //将套餐id添加到套餐菜品中
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealMapper.addSetmealDish(setmealDish);
        }


    }

    //根据id查询套餐 回显
    @Override
    public SetmealDTO selectById(Long id) {
        SetmealDTO sd = new SetmealDTO();
        //查询套餐并添加到集合中
        Setmeal setmeal = setmealMapper.selectSetmealById(id);
        BeanUtils.copyProperties(setmeal,sd);
        //查询套餐对应的菜品并添加到集合中
        List<SetmealDish> setmealDishes = setmealMapper.selectSetmealDishById(id);
        sd.setSetmealDishes(setmealDishes);
        return sd;
    }
}